<?php

namespace app\admins\controller;

class Group extends Base
{
    // Curd复用
    use Curd;

    // 初始化
    protected function initialize()
    {
        parent::initialize();
        $this->model = $this->admin_group;
        $this->sort = ['id' => 'desc'];
        $this->is_page = false;
        $this->rule = [
            'title|角色名称' => 'require|token|unique:admin_group', //验证令牌且值唯一
        ];

        // 树形组件赋值
        if (in_array($this->action, ['add', 'edit'])) {
            $menus = $this->admin_menu
                ->field('id, pid, title')
                ->where('status', 0)
                ->order(['ord' => 'desc', 'id' => 'asc'])
                ->select()->toArray();
            $menus = $this->levels($menus);
            $menus = json_encode($menus);
            $this->app->view->assign('menus', $menus);
        }

        // 树形组件接收
        if (in_array($this->action, ['save', 'save_edit'])) {
            $this->param['rights'] = json_encode(array_map('intval', array_values($this->param['tree'] ?? [])));
        }
    }
}
